package com.example.safeassistan.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.safeassistan.UserInfoModel;
import com.example.safeassistan.base.MyApp;
import com.example.safeassistan.login.model.LoginModel;
import com.example.safeassistan.utile.StringUtils;

public class UserData {
    private Context context;
    private static UserData UD = null;//会员业务逻辑类 对象
    public static final String USER_FILE_NAME = "user_info";//会员信息文件名


    public static final String ACCOUNT="account";//账号
    public static final String PHONE="phone";//手机号
    public static final String AVATAR = "headLink";//头像地址
    public static final String UID = "id";//用户id
    public static final String NICKNAME = "nickname";//昵称
    public static final String REALNAME = "real_name";//真实姓名
    public static final String DEPARTMENTNAME="departName";//部门名称
    public static final String DEPARTID="departId";//部门id
    public static final String POSTNAME="postName";
    public static final String PASSWORD="password";
    public static final String companyImg="companyImg";//所在公司logo
    public static final String companyName="companyName";//所在公司名称

    public static final String ls_ps="ls_ps";//隐患评审流程
    public static final String ls_lec="ls_lec";//风险评估方法
    public static final String ls_xj="ls_xj";//巡检设备开关
    public static final String ls_jc="ls_jc";//显示检查地址
    public static final String ls_yh="ls_yh";//整改方案


    private UserData(Context context) {
        this.context = context;
    }

    /**
     * 单利模式
     *
     * @param context
     * @return
     */
    public static synchronized UserData create(Context context) {
        return create();
    }

    /**
     * 单利模式
     *
     * @return
     */
    public static synchronized UserData create() {
        if (UD == null)
            UD = new UserData(MyApp.context);
        return UD;
    }

    /**
     * 改变登陆状态  清空保存数据
     *
     * @param islogin
     * @return
     */
    public UserData ChangeLoginState(boolean islogin) {
        //退出登陆
        if (!islogin) {
            clearAll();
        }
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.commit();
        return UD;
    }

    private void clearAll() {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.clear();
        ed.commit();
    }

    /**
     * 获取数据(String类型)
     *
     * @param key
     * @return
     */
    public String get(String key) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, null);
    }

    /**
     * 获取数据(int类型)
     *
     * @param key
     * @return
     */
    public int get(String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    /**
     * 获取数据(long类型)
     *
     * @param key
     * @return
     */
    public long getL(String key, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

    /**
     * 保存数据
     *
     * @param userBean
     * @return
     */
    public boolean saveUserData(UserInfoModel.ResultDTO userBean) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putString(ls_jc, userBean.getConfigsVOS().get(3).getValueName());
        } catch (Exception e) {
            ed.putString(ls_jc, "");
        }

        try {
            ed.putString(ls_yh, userBean.getConfigsVOS().get(4).getValueName());
        } catch (Exception e) {
            ed.putString(ls_yh, "");
        }


        try {
            ed.putString(ls_ps, userBean.getConfigsVOS().get(2).getValueName());
        } catch (Exception e) {
            ed.putString(ls_ps, "");
        }
        try {
            ed.putString(ls_lec, userBean.getConfigsVOS().get(0).getValueName());
        } catch (Exception e) {
            ed.putString(ls_lec, "");
        }

        try {
            ed.putString(ls_xj, userBean.getConfigsVOS().get(1).getValueName());
        } catch (Exception e) {
            ed.putString(ls_xj, "");
        }

        try {
            ed.putString(UID, userBean.getId());
        } catch (Exception e) {
            ed.putString(UID, "");
        }

        try {
            ed.putString(companyName, userBean.getQymc());
        } catch (Exception e) {
            ed.putString(companyName, "");
        }

        try {
            ed.putString(companyImg, userBean.getCompanyImg());
        } catch (Exception e) {
            ed.putString(companyImg, "");
        }

        try {
            ed.putString(DEPARTMENTNAME, userBean.getDepartName());
        } catch (Exception e) {
            ed.putString(DEPARTMENTNAME, "");
        }

        try {
            ed.putString(PASSWORD, userBean.getPassword());
        } catch (Exception e) {
            ed.putString(PASSWORD, "");
        }


        try {
            ed.putString(POSTNAME, userBean.getPostName());
        } catch (Exception e) {
            ed.putString(POSTNAME, "");
        }

        try {
            ed.putString(PHONE, userBean.getPhone());
        } catch (Exception e) {
            ed.putString(PHONE, "");
        }


        try {
            ed.putString(REALNAME, userBean.getRealname());
        } catch (Exception e) {
            ed.putString(REALNAME, "");
        }

        try {
            ed.putString(DEPARTID, userBean.getDepartIds());
        } catch (Exception e) {
            ed.putString(DEPARTID, "");
        }

        try {
            ed.putString(NICKNAME, userBean.getUsername());
        } catch (Exception e) {
            ed.putString(NICKNAME, "");
        }

        try {
            ed.putString(ACCOUNT, StringUtils.getString(userBean.getUsername()));
        } catch (Exception e) {
            ed.putString(ACCOUNT, "");
        }

        try {
            ed.putString(AVATAR, StringUtils.getString(userBean.getAvatar()));
        } catch (Exception e) {
            ed.putString(AVATAR, "");
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putString(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putInt(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putLong(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }
}
